import json
import sys
from logicaltool.BaseTools.file_tools import FileTools
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
configfile = BaseDir.get_new_path(curPath, "db.ini")


class DealFile(FileTools):

    def __init__(self):
        super().__init__()
        self.dicts = {}
        self.dicts2 = {}

    def deal_mysql_select(self, rows):
        for row in rows:
            self.dicts[row[0]] = row[1]

    def get_from_file(self):
        for line in BaseDir.read_file_r_mode_yield(r"D:\download\sipogov\download\detail2\all.big_html"):
            dictsr = {}
            dictsreuslt = json.loads(line)
            pub_no = dictsreuslt["abstractInfoDTO"]["pn"]
            print(pub_no)
            if pub_no in self.dicts:
                jsonmsg = self.dicts[pub_no]
                dictsr["jsonmsg"] = json.loads(jsonmsg)
                dictsr["detail"] = dictsreuslt
                linewrite = json.dumps(dictsr, ensure_ascii=False)
                print(linewrite)
                BaseDir.single_add_file("./all.big_json", linewrite + "\n")
            else:
                print("*************")
                print(pub_no)
                sys.exit(-1)


dealfile = DealFile()
dealfile.set_config_file(configfile)
dealfile.conn_mysql()
sql = "select pub_no, jsonmsg from search"
dealfile.set_sql(sql)
rows = dealfile.mysql_select()
dealfile.deal_mysql_select(rows)
dealfile.get_from_file()
